import { _decorator, BoxCollider2D, Component, Contact2DType, Director, director, find, Node } from 'cc';
import { lookAtTarget, targetLineMove } from './utils';
import { tagEnum } from './Enum';
import { GameMgr } from './GameMgr';
const { ccclass, property } = _decorator;

@ccclass('EnemyBullet')
export class EnemyBullet extends Component {

    speed = 10;

    init(_tarPos) {
        console.log("生成子弹")
        lookAtTarget(find("Canvas/playerRoot").position, this.node, 30);
        targetLineMove(_tarPos, this.node, this.speed);
        this.node.getComponent(BoxCollider2D).on(Contact2DType.BEGIN_CONTACT, this.beginTouch,this)
    }

    beginTouch(self, other) {
        director.once(Director.EVENT_AFTER_PHYSICS, () => {
            if (other.tag == tagEnum.player) {
                GameMgr.ins.PlayerHp -= 1;

                this.node.destroy();
            }

        })

    }


    protected update(dt: number): void {
        // if (Math.abs(this.node.position.x) > 2000 || Math.abs(this.node.position.y) > 2000) {
        //     this.node.destroy();
        // }
    }


}


